package com.ruoyi.biz.service.impl;

import com.ruoyi.biz.entity.LotteryLogDO;
import com.ruoyi.biz.entity.LotteryUserDO;
import com.ruoyi.biz.entity.WechatUserDO;
import com.ruoyi.biz.mapper.LotteryUserDAO;
import com.ruoyi.biz.mapper.WechatUserDAO;
import com.ruoyi.biz.service.WechatUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WechatUserServiceImpl implements WechatUserService {

    @Resource
    WechatUserDAO wechatUserDAO;
    @Resource
    LotteryUserDAO lotteryUserDAO;

    @Override
    public List<WechatUserDO> queryList(WechatUserDO wechatUserDO) {
        return  wechatUserDAO.queryList(wechatUserDO);
    }

    @Override
    public int insert(WechatUserDO wechatUserDO) {
        return wechatUserDAO.insert(wechatUserDO);
    }

    @Override
    public int update(WechatUserDO wechatUserDO) {
        return wechatUserDAO.update(wechatUserDO);
    }

    @Override
    @Transactional
    public int addBind(WechatUserDO wechatUserDO, LotteryUserDO lotteryUserDO) {
        WechatUserDO qWechatUserDO = wechatUserDAO.queryById(wechatUserDO.getUserId());

        lotteryUserDO.setUserId(wechatUserDO.getUserId());
        lotteryUserDO.setSex(qWechatUserDO.getSex());
        lotteryUserDO.setRecAddr(wechatUserDO.getRecAddr());
        int k = lotteryUserDAO.update(lotteryUserDO);
        if (k > 0) {
            k = wechatUserDAO.update(wechatUserDO);
        }
        return k;
    }

    @Override
    public int removeBind(String lotteryUserId, String userId) {
        int k = lotteryUserDAO.removeBind(lotteryUserId);
        if (k > 0) {
            k = wechatUserDAO.removeBind(userId);
        }
        return k;
    }
}
